from celery_tasks.main import app
from django.core.mail import send_mail
from django.conf import settings


# bind: 会将第一个参数绑定到函数的第一参数
# name: 表示任务的名称
@app.task(bind=True, name="send_email")
def send_email(self, to_email, verify_url):
    try:
        subject = '美多商城邮箱验证'
        html_message = '<p>尊敬的用户你好</p>' \
                       '<p>欢迎你使用美多商城</p>' \
                       '<p>您的邮箱为：%s 。请点击此链接激活您的邮箱：</p>' \
                       '<p><a href="%s">%s<a></p>' % (to_email, verify_url, verify_url)
        send_mail(subject=subject, message='', from_email=settings.EMAIL_FROM, recipient_list=[to_email],
                  html_message=html_message)
    except Exception as e:
        raise self.retry(countdown=3, max_retries=3, exc=Exception('发送邮件失败了'))
